import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_dk_form/flutter_dk_form.dart';

class DkSimpleFormDemoPage extends StatefulWidget {
  static const String routeName = '/page/dkformfeild_demo_page';
  static const String name = "DkFormFieldSimpleDemoPage";

  @override
  DkSimpleFormDemoState createState() => DkSimpleFormDemoState();
}

class DkSimpleFormDemoState extends State<DkSimpleFormDemoPage> {
  GlobalKey<DkFormState> _formKey = GlobalKey<DkFormState>();
  void _forSubmitted() {
    var _form = _formKey.currentState;
    if (_form.validate(selfShowError: false)) {
      _form.save();
      print(json.encode(_form.value));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DkFormFieldDemoPage'),
      ),
      floatingActionButton: FloatingActionButton(
        child: Text('提交'),
        onPressed: _forSubmitted,
      ),
      body: SingleChildScrollView(
        child: DkForm(
          key: _formKey,
          autovalidate: false,
          child: Column(
            children: <Widget>[
              DkTextFormField(
                attribute: 'name',
                initialValue: 'jim',
              ),
              DkCheckBoxFormField(
                attribute: 'agree',
                title: const Text('同意协议'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
